# import pygame
# from pygame.locals import *
# from sys import exit
# pygame.init()
# screen = pygame.display.set_mode((400,300))
# pygame.display.set_caption("Draw Rectangle with Mouse")
# screen.fill((127,127,127))
# x=0
# y=0
# w=0
# h=0
# drawmode=True
# running = True
# while running:
#    for event in pygame.event.get():
#       if event.type == QUIT:
#          running = False
#       if event.type == MOUSEBUTTONDOWN:
#          x,y = pygame.mouse.get_pos()
#          drawmode = True
#       if event.type == MOUSEBUTTONUP:
#          x1,y1 = pygame.mouse.get_pos()
#          w=x1-x
#          h=y1-y
#          drawmode= False
#
#    rect = pygame.Rect(x,y,w,h)
#    if drawmode == False:
#       pygame.draw.rect(screen, (255,0,0), rect)
#    pygame.display.flip()
# pygame.quit()
import pygame
from pygame.locals import *
from sys import exit

pygame.init()
screen = pygame.display.set_mode((400, 300))
rect = Rect(50, 60, 200, 80)
moving = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        elif event.type == MOUSEBUTTONDOWN:
            if rect.collidepoint(event.pos):
                moving = True
        elif event.type == MOUSEBUTTONUP:
            moving = False
        elif event.type == MOUSEMOTION and moving:
            rect.move_ip(event.rel)
    screen.fill((127, 127, 127))
    pygame.draw.rect(screen, (255, 0, 0), rect)
    if moving:
        pygame.draw.rect(screen, (0, 0, 255), rect, 4)
    pygame.display.flip()
pygame.quit()